import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

/* Layout */
import Layout from '@/layout';

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin', 'shopkeeper'] control the page roles(you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [{
  path: '/user/:type',
  component: () => import('@/views/user/index'),
  hidden: true
},

{
  path: '/',
  component: Layout,
  redirect: '/home',
  children: [{
    path: 'home',
    name: 'Home',
    component: () => import('@/views/home/index'),
    meta: {
      title: '首页',
      icon: 'home'
    }
  }]
},

{
  path: '/chat',
  component: Layout,
  hidden: true,
  children: [{
    path: '/',
    name: 'Chat',
    component: () => import('@/views/chat'),
    meta: {
      title: '在线客服'
    }
  }]
}

];

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [{
  path: '/shop',
  component: Layout,
  redirect: '/shop/information',
  name: 'Shop',
  meta: {
    title: '店铺管理',
    icon: 'shop',
    roles: ['shopkeeper']
  },
  children: [{
    path: 'information',
    name: 'Information',
    component: () => import('@/views/shop/information'),
    meta: {
      title: '店铺信息'
    }
  }, {
    path: 'auth_credit',
    name: 'ShopAuthCredit',
    component: () => import('@/views/shop/auth_credit'),
    meta: {
      title: '诚信认证'
    }
  }, {
    path: 'auth_brand',
    name: 'ShopAuthBrand',
    component: () => import('@/views/shop/auth_brand'),
    meta: {
      title: '品牌认证'
    }
  }, {
    path: 'album',
    name: 'Album',
    component: () => import('@/views/shop/album/index'),
    meta: {
      title: '企业相册'
    }
  }, {
    path: 'detail/:id',
    name: 'AlbumDetail',
    component: () => import('@/views/shop/album/detail'),
    hidden: true,
    meta: {
      title: '相册详情'
    }
  }]
},
{
  path: '/notice',
  component: Layout,
  hidden: true,
  meta: {
    roles: ['shopkeeper']
  },
  children: [{
    path: '/',
    name: 'Notice',
    component: () => import('@/views/others/notice/index'),
    meta: {
      title: '平台通知'
    }
  }, {
    path: 'detail',
    name: 'NoticeDetail',
    component: () => import('@/views/others/notice/detail'),
    meta: {
      title: '公告通知'
    }
  }]
},
{
  path: '/goods',
  component: Layout,
  name: 'Goods',
  redirect: '/goods/list',
  meta: {
    title: '商品管理',
    icon: 'goods',
    roles: ['shopkeeper']
  },
  children: [{
    path: 'list',
    name: 'GoodsList',
    component: () => import('@/views/goods/list'),
    meta: {
      title: '商品管理'
    }
  }, {
    path: 'add',
    name: 'GoodsAdd',
    hidden: true,
    component: () => import('@/views/goods/addProduct'),
    meta: {
      title: '商品添加'
    }
  }, {
    path: 'detail',
    name: 'GoodsDetail',
    hidden: true,
    component: () => import('@/views/goods/productDetail'),
    meta: {
      title: '商品详情'
    }
  }, {
    path: 'ranking',
    name: 'GoodsRanking',
    hidden: true,
    component: () => import('@/views/goods/ranking'),
    meta: {
      title: '销售商品排行'
    }
  }]
},
{
  path: '/order',
  component: Layout,
  redirect: '/order/list',
  name: 'Order',
  meta: {
    title: '订单管理',
    icon: 'order',
    roles: ['shopkeeper']
  },
  children: [{
    path: '/order/list',
    name: 'OrderList',
    component: () => import('@/views/order/list'),
    meta: {
      title: '订单列表'
    }
  }, {
    path: 'after_sale',
    name: 'OrderAfterSale',
    component: () => import('@/views/order/after_sale'),
    meta: {
      title: '售后退款'
    }
  },
  {
    path: 'detail',
    name: 'DetailList',
    hidden: true,
    component: () => import('@/views/order/components/list/detail'),
    meta: {
      title: '订单详情'
    }
  },
  {
    path: 'detail_after',
    name: 'DetailAfter',
    hidden: true,
    component: () => import('@/views/order/components/after_sale/detail'),
    meta: {
      title: '售后订单详情'
    }
  }]
},

{
  path: '/activities',
  component: Layout,
  redirect: '/activities/ad',
  name: 'Activities',
  meta: {
    title: '活动管理',
    icon: 'activities',
    roles: ['shopkeeper']
  },
  children: [{
    path: 'ad',
    name: 'AdShop',
    component: () => import('@/views/activities/shop/ad/index'),
    meta: {
      title: '店铺广告'
    }
  }, {
    path: 'ad/detail/:id',
    name: 'AdDetail',
    hidden: true,
    component: () => import('@/views/activities/shop/ad/detail'),
    meta: {
      title: '广告详情'
    }
  }, {
    path: 'ad/edit/:id',
    name: 'AdEdit',
    hidden: true,
    component: () => import('@/views/activities/shop/ad/edit'),
    meta: {
      title: '广告编辑'
    }
  }, {
    path: 'ad/add',
    name: 'AdAdd',
    hidden: true,
    component: () => import('@/views/activities/shop/ad/add'),
    meta: {
      title: '添加广告'
    }
  }, {
    path: 'shop',
    name: 'ActivitiesShop',
    component: () => import('@/views/activities/shop/activities/index'),
    meta: {
      title: '店铺活动'
    }
  }, {
    path: 'shop/detail/:id',
    name: 'ShopActivityDetail',
    hidden: true,
    component: () => import('@/views/activities/shop/activities/detail'),
    meta: {
      title: '活动详情'
    }
  }, {
    path: 'shop/edit/:id',
    name: 'ShopActivityEdit',
    hidden: true,
    component: () => import('@/views/activities/shop/activities/edit'),
    meta: {
      title: '活动编辑'
    }
  }, {
    path: 'shop/add',
    name: 'ShopActivityAdd',
    hidden: true,
    component: () => import('@/views/activities/shop/activities/add'),
    meta: {
      title: '添加活动'
    }
  }, {
    path: 'admin',
    name: 'ActivitiesAdmin',
    component: () => import('@/views/activities/admin/index'),
    meta: {
      title: '平台活动'
    }
  }, {
    path: 'admin/sign/:id',
    name: 'ActivitySign',
    hidden: true,
    component: () => import('@/views/activities/admin/sign'),
    meta: {
      title: '活动报名'
    }
  }, {
    path: 'coupon',
    name: 'CouponList',
    component: () => import('@/views/activities/coupon'),
    meta: {
      title: '优惠券列表'
    }
  }]
},

{
  path: '/finance',
  component: Layout,
  redirect: '/finance/binding',
  name: 'Finance',
  meta: {
    title: '财务管理',
    icon: 'finance',
    roles: ['shopkeeper']
  },
  children: [{
    path: 'binding',
    name: 'Binding',
    component: () => import('@/views/finance/binding'),
    meta: {
      title: '绑定提现账户'
    }
  }, {
    path: 'cash',
    name: 'Cash',
    component: () => import('@/views/finance/cash'),
    meta: {
      title: '提现申请'
    }
  }, {
    path: 'statistics',
    name: 'Statistics',
    component: () => import('@/views/finance/statistics'),
    meta: {
      title: '数据统计'
    }
  }]
},

{
  path: '/comments',
  component: Layout,
  name: 'Comments',
  redirect: '/comments/list',
  meta: {
    title: '评价管理',
    icon: 'zan',
    roles: ['shopkeeper']
  },
  children: [{
    path: 'list',
    name: 'CommentsList',
    component: () => import('@/views/comments/list'),
    meta: {
      title: '评价管理'
    }
  }, {
    path: 'detail',
    name: 'CommentsDetail',
    hidden: true,
    component: () => import('@/views/comments/detail'),
    meta: {
      title: '评价详情'
    }
  }]
},

{
  path: '/tender',
  component: Layout,
  redirect: '/tender/list',
  meta: {
    title: '招标大厅',
    icon: 'tender',
    roles: ['shopkeeper']
  },
  children: [{
    path: 'list',
    name: 'TenderList',
    component: () => import('@/views/tender/index'),
    meta: {
      title: '招标大厅'
    }
  }, {
    path: 'detail',
    name: 'TenderDetail',
    hidden: true,
    component: () => import('@/views/tender/detail'),
    meta: {
      title: '招标详情'
    }
  }]
},
// 修改密码
{
  path: '/password',
  name: 'Password',
  component: Layout,
  redirect: '/password/modify',
  hidden: true,
  meta: {
    roles: ['shopkeeper']
  },
  children: [{
    path: 'modify',
    name: 'PasswordModify',
    hidden: true,
    component: () => import('@/views/others/password/modify'),
    meta: {
      title: '修改密码'
    }
  }]
},
// 修改修改退换货地址
{
  path: '/refund',
  name: 'Refund',
  component: Layout,
  redirect: '/refund/index',
  hidden: true,
  meta: {
    roles: ['shopkeeper']
  },
  children: [{
    path: 'index',
    name: 'RefundAddress',
    component: () => import('@/views/others/refundAddress/index'),
    meta: {
      title: '退货地址'
    }
  }]
},
{
  path: '/members',
  component: Layout,
  hidden: true,
  children: [{
    path: '/',
    name: 'Members',
    component: () => import('@/views/others/members'),
    meta: {
      title: '会员服务',
      roles: ['shopkeeper']
    }
  }]
}, {
  path: '/message',
  component: Layout,
  hidden: true,
  children: [{
    path: '/',
    name: 'Message',
    component: () => import('@/views/others/message'),
    meta: {
      title: '系统消息',
      roles: ['shopkeeper']
    }
  }]
}, {
  path: '/task',
  component: Layout,
  hidden: true,
  children: [{
    path: '/',
    name: 'Task',
    component: () => import('@/views/others/task'),
    meta: {
      title: '新手任务',
      roles: ['shopkeeper']
    }
  }]
},
// 404 page must be placed at the end !!!
{
  path: '*',
  name: '404',
  redirect: '/404',
  hidden: true
}
];

const createRouter = () => new Router({
  mode: 'history', // require service support
  base: process.env.NODE_ENV === 'development' ? '/' : '/merchant',
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRoutes
});

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
